﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading;

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;

namespace mvcForum.Tests.WebUI {

	public static class TestHelper {

		static TestHelper() {
			//siteBaseURL = ConfigurationManager.AppSettings["SiteBaseURL"];
			//if (!siteBaseURL.EndsWith("/")) {
			//    siteBaseURL += "/";
			//}
			//siteBaseURL = siteBaseURL.ToLower();
		}

		//private static String siteBaseURL;
		//public static String SiteBaseURL {
		//    get {
		//        return siteBaseURL;
		//    }
		//}

		public static IWebDriver GetDriver() {
			//_driver = new InternetExplorerDriver();
			//IWebDriver driver = new ChromeDriver();
			IWebDriver driver = new FirefoxDriver();

			// this will make the driver wait 10 seconds when looking for an element (to allow for submits)
			driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 5));
			return driver;
		}


		public static void Submit(this IWebDriver driver, String value) {
			IWebElement box;
			try {
				box = driver.FindElement(By.XPath("//input[@type='submit' and @value='" + value + "']"));
			}
			catch {
				box = driver.FindElement(By.XPath("//button[@type='submit']"));
			}
			if (box == null) {
				// TODO:
			}
			box.Click();
		}


		public static void Wait(this IWebDriver driver, Int32 seconds) {
			Thread.Sleep(1000 * seconds);
		}
	}
}